/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GestionarEmpleadoUI.java
 *
 * Created on 12/06/2009, 11:02:43 AM
 */

package CRM_UI;
import DAO.*;
import LogicaNegocio.*;
import java.awt.Cursor;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author L¡z
 */
public class GestionarRSC_UI extends javax.swing.JFrame {

    Vector <Vector> rscTabla;
    Vector <String> nombresColumnas;
    String idSeleccionado;
    JFrame objCoordinadorSC_UI;
    String departamentos;
    Cursor handCursor = new Cursor(Cursor.HAND_CURSOR);
    Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
    
    public GestionarRSC_UI(JFrame objCoordSc_UI, String departamento) {
        this.objCoordinadorSC_UI = objCoordSc_UI;
        rscTabla = new Vector <Vector>();
        idSeleccionado = " ";
        this.departamentos = departamento;

        incializarColumnas();
        consultarRSC();
        initComponents();

        

        tablaRsc.setModel(new DefaultTableModel (rscTabla, nombresColumnas));
    }

    public void actualizarTabla()
    {
        tablaRsc.setModel(new DefaultTableModel (rscTabla, nombresColumnas));
    }

  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jefesPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaRsc = new javax.swing.JTable();
        gestionarPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        textFieldNombre = new javax.swing.JTextField();
        textFieldCedula = new javax.swing.JTextField();
        modificarButton = new javax.swing.JToggleButton();
        eliminarButton = new javax.swing.JToggleButton();
        botonCrear = new javax.swing.JToggleButton();
        casitaBoton = new javax.swing.JLabel();
        botonLimpiar = new javax.swing.JButton();

        jefesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Empleados"));

        /*
        tablaRsc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        *//*
        tablaRsc.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        */
        tablaRsc.setAutoscrolls(true);
        tablaRsc.setCellSelectionEnabled(true);
        tablaRsc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaRscMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablaRsc);
        tablaRsc.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        javax.swing.GroupLayout jefesPanelLayout = new javax.swing.GroupLayout(jefesPanel);
        jefesPanel.setLayout(jefesPanelLayout);
        jefesPanelLayout.setHorizontalGroup(
            jefesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jefesPanelLayout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 392, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(52, Short.MAX_VALUE))
        );
        jefesPanelLayout.setVerticalGroup(
            jefesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jefesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
        );

        gestionarPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Gestionar Empleado"));

        jLabel1.setText("Nombre:");

        jLabel2.setText("Cédula:");

        modificarButton.setText("modificar");
        modificarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarButtonActionPerformed(evt);
            }
        });

        eliminarButton.setText("eliminar");
        eliminarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarButtonActionPerformed(evt);
            }
        });

        botonCrear.setText("crear");
        botonCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCrearActionPerformed(evt);
            }
        });

        casitaBoton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/home.png"))); // NOI18N
        casitaBoton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                casitaBotonMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                casitaBotonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                casitaBotonMouseExited(evt);
            }
        });

        botonLimpiar.setText("limpiar");
        botonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonLimpiarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout gestionarPanelLayout = new javax.swing.GroupLayout(gestionarPanel);
        gestionarPanel.setLayout(gestionarPanelLayout);
        gestionarPanelLayout.setHorizontalGroup(
            gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gestionarPanelLayout.createSequentialGroup()
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(gestionarPanelLayout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)))
                    .addGroup(gestionarPanelLayout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(modificarButton)))
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(gestionarPanelLayout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(eliminarButton)
                        .addGap(29, 29, 29)
                        .addComponent(botonCrear)
                        .addGap(31, 31, 31)
                        .addComponent(botonLimpiar))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gestionarPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(gestionarPanelLayout.createSequentialGroup()
                                .addComponent(textFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gestionarPanelLayout.createSequentialGroup()
                                .addComponent(textFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(48, 48, 48)))))
                .addContainerGap(94, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gestionarPanelLayout.createSequentialGroup()
                .addContainerGap(431, Short.MAX_VALUE)
                .addComponent(casitaBoton))
        );
        gestionarPanelLayout.setVerticalGroup(
            gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gestionarPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(55, 55, 55)
                .addGroup(gestionarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(modificarButton)
                    .addComponent(eliminarButton)
                    .addComponent(botonCrear)
                    .addComponent(botonLimpiar))
                .addContainerGap(30, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, gestionarPanelLayout.createSequentialGroup()
                .addContainerGap(117, Short.MAX_VALUE)
                .addComponent(casitaBoton))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(gestionarPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jefesPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jefesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addComponent(gestionarPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void incializarColumnas()
    {
        nombresColumnas = new Vector <String> ();
        nombresColumnas.addElement("Cedula");
        nombresColumnas.addElement("Nombre");
        nombresColumnas.addElement("Departamento");
        nombresColumnas.addElement("Jefe");
        
    }

     public void consultarRSC()
    {
        rscTabla.clear();
        DAOEmpleado daoEmpleado = new DAOEmpleado();
        Vector <Empleado> vecEmpleado = null;
        if(departamentos.equals("SC")) {
            vecEmpleado = daoEmpleado.consultarRSC();
        }
        else {
            vecEmpleado = daoEmpleado.consultarRrepresentanteVentas();
        }

        System.out.println("******size vector******** "+vecEmpleado.size());
        
        for(int i=0; i<vecEmpleado.size(); i++)
        {
            Vector <String> empleado = new Vector <String>();
            empleado.addElement(vecEmpleado.elementAt(i).getCcEmpleado());
            empleado.addElement(vecEmpleado.elementAt(i).getNombreEmpleado());
            empleado.addElement(vecEmpleado.elementAt(i).getDepartamento().getNombreDepartamento());
            empleado.addElement(vecEmpleado.elementAt(i).getId_jefe());

            rscTabla.addElement(empleado);
        }
        System.out.println("total representantes: "+rscTabla.size());
    }
    private void modificarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarButtonActionPerformed
        DAOEmpleado daoEmpleado= new DAOEmpleado();
        String idJefe= "";
        Departamento departamento=null;
        if(departamentos.equals("SC")) {
            idJefe=  daoEmpleado.retornarIdJefeSc();
            departamento= new Departamento("Servicio al Cliente");
        }
        else {
            idJefe=  daoEmpleado.retornarIdJefeVentas();
            departamento= new Departamento("Ventas");
        }
        Empleado empleado= new Empleado(textFieldCedula.getText(), textFieldNombre.getText(), departamento, idJefe);
        int i= daoEmpleado.modificarNombreEmpleado(empleado);

        if(i==1)
            {
                JOptionPane.showMessageDialog(null, "Se ha modificado "+i+" Empleado", "Empleado modificado", JOptionPane.INFORMATION_MESSAGE);
            }
            else{
                JOptionPane.showMessageDialog(this, "Error de Modificación", "Empleado no modificado", JOptionPane.ERROR_MESSAGE);
            }

            System.out.print("Se han borrado " + i + " empleados de la BD\n");
            
            consultarRSC();
            actualizarTabla();

}//GEN-LAST:event_modificarButtonActionPerformed

    private void eliminarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarButtonActionPerformed
        int option = JOptionPane.showConfirmDialog(null, "¿Está seguro que desea borrar el empleado?", "Eliiminar Empleado" , JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

        if(option == 0)
        {
            //int fila = tablaRsc.getSelectedRow();
            //idSeleccionado = tablaRsc.getValueAt(fila, 0).toString();
            DAOEmpleado daoEmpleado= new DAOEmpleado();
            int i= daoEmpleado.eliminarEmpleado(idSeleccionado);

            if(i==1)
            {
                JOptionPane.showMessageDialog(null, "Se ha eliminado "+i+" Empleado", "Empleado eliminado", JOptionPane.INFORMATION_MESSAGE);
            }
            else{
                JOptionPane.showMessageDialog(this, "Error de Eliminación", "Empleado no eliminado", JOptionPane.ERROR_MESSAGE);
            }
            
            System.out.print("Se han borrado " + i + " empleados de la BD\n");
            
        }

        consultarRSC();
        actualizarTabla();
}//GEN-LAST:event_eliminarButtonActionPerformed

    private void botonCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCrearActionPerformed
        
        DAOEmpleado daoEmpleado= new DAOEmpleado();
        String idJefe= "";
        Departamento departamento= null;
        if(departamentos.equals("SC")) {
            idJefe= daoEmpleado.retornarIdJefeSc();
            departamento= new Departamento("Servicio al Cliente");
        }
        else {
            idJefe= daoEmpleado.retornarIdJefeVentas();
            departamento= new Departamento("Ventas");
        }
       
        System.out.println("****** jefe **** "+idJefe);
        

        Empleado empleado= new Empleado(textFieldCedula.getText(), textFieldNombre.getText(), departamento, idJefe);
        int i= daoEmpleado.guardarEmpleado(empleado);

        if(i==1)
        {
            JOptionPane.showMessageDialog(null, "Se ha insertado "+i+" nuevo Empleado", "Nuevo Empleado", JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            JOptionPane.showMessageDialog(this, "Error de Inserción", "Empleado no insertado", JOptionPane.ERROR_MESSAGE);
        }
        
        System.out.print("Se han insertado " + i + " Registro a la BD\n");

        consultarRSC();
        actualizarTabla();
}//GEN-LAST:event_botonCrearActionPerformed

    private void tablaRscMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaRscMouseClicked
        int fila = tablaRsc.getSelectedRow();
        idSeleccionado = tablaRsc.getValueAt(fila, 0).toString();
        textFieldCedula.setText(idSeleccionado);
        textFieldNombre.setText(tablaRsc.getValueAt(fila,1).toString());
        

    }//GEN-LAST:event_tablaRscMouseClicked

    private void casitaBotonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_casitaBotonMouseClicked
        objCoordinadorSC_UI.setVisible(true);
        this.setVisible(false);
}//GEN-LAST:event_casitaBotonMouseClicked

    private void casitaBotonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_casitaBotonMouseEntered
        this.setCursor(handCursor);
    }//GEN-LAST:event_casitaBotonMouseEntered

    private void casitaBotonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_casitaBotonMouseExited
        this.setCursor(defaultCursor);
    }//GEN-LAST:event_casitaBotonMouseExited

    private void botonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonLimpiarActionPerformed
        textFieldCedula.setText("");
        textFieldNombre.setText("");

    }//GEN-LAST:event_botonLimpiarActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton botonCrear;
    private javax.swing.JButton botonLimpiar;
    private javax.swing.JLabel casitaBoton;
    private javax.swing.JToggleButton eliminarButton;
    private javax.swing.JPanel gestionarPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jefesPanel;
    private javax.swing.JToggleButton modificarButton;
    private javax.swing.JTable tablaRsc;
    private javax.swing.JTextField textFieldCedula;
    private javax.swing.JTextField textFieldNombre;
    // End of variables declaration//GEN-END:variables

}
